﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.Library.Parts;
using AnguaRPGEngine.Library.Interfaces;
using log4net;
using System.Reflection;
using AnguaRPGEngine.Library.Systems;

namespace AnguaRPGEngine.MapEditor
{
    public partial class MapContainer : Form
    {
        private static readonly ILog _Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private Library.Controls.MapControl mapControl1;
        private string _MapName;
        public string MapName
        {
            get
            {
                return _MapName;
            }
            set
            {
                _MapName = value;
                this.Text = value;
            }
        }
        public string MapFileName;
        public List<IPart> Map
        {
            get
            {
                return this.mapControl1.Parts;
            }
            set
            {
                this.mapControl1.Parts = value;
            }
        }
        public MapContainer()
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                InitializeComponent();
                this.mapControl1 = new Library.Controls.MapControl(true);
                this.mapControl1.Dock = DockStyle.Fill;
                this.mapControl1.Parent = this;
                this.mapControl1.Visible = true;
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }

        private void MapContainer_Load(object sender, EventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }

        private void MapContainer_FormClosing(object sender, FormClosingEventArgs e)
        {
            string MethodName = MethodBase.GetCurrentMethod().Name;
            try
            {
                _Log.DebugFormat("{0} - Entry", MethodName);
                mapControl1.Shutdown();
            }
            catch (Exception err)
            {
                _Log.Error(MethodName, err);
            }
            finally
            {
                _Log.DebugFormat("{0} - Exit", MethodName);
            }
        }

        public void SaveMap()
        {
            MapFileClass MapFile = new MapFileClass();
            List<IPart> floor = this.mapControl1.Parts.FindAll(p => p.PartType == PartTypes.Floor);
            List<IPart> door = this.mapControl1.Parts.FindAll(p => p.PartType == PartTypes.Door);
            List<IPart> scenery = this.mapControl1.Parts.FindAll(p => p.PartType == PartTypes.Scenery);
            List<IPart> counter = this.mapControl1.Parts.FindAll(p => p.PartType == PartTypes.Counter);
            List<IPart> effect = this.mapControl1.Parts.FindAll(p => p.PartType == PartTypes.Effect);
            List<IPart> wall = this.mapControl1.Parts.FindAll(p => p.PartType == PartTypes.Wall);
            List<IPart> markup = this.mapControl1.Parts.FindAll(p => p.PartType == PartTypes.Markup);
            MapFile.Floors = new List<Floor>();
            foreach (Floor fl in floor)
            {
                MapFile.Floors.Add(fl);
            }
            MapFile.Scenery = new List<Scenery>();
            foreach (Scenery sc in scenery)
            {
                MapFile.Scenery.Add(sc);
            }
            MapFile.Counters = new List<Counter>();
            foreach (Counter co in counter)
            {
                MapFile.Counters.Add(co);
            }
            MapFile.Doors = new List<Door>();
            foreach (Door dr in door)
            {
                MapFile.Doors.Add(dr);
            }
            MapFile.Walls = new List<Wall>();
            foreach (Wall w in wall)
            {
                MapFile.Walls.Add(w);
            }
            MapFile.Effects = new List<Effect>();
            foreach (Effect ef in effect)
            {
                MapFile.Effects.Add(ef);
            }
            MapFile.Markups = new List<Markup>();
            foreach (Markup mu in markup)
            {
                MapFile.Markups.Add(mu);
            }
            Serializer<MapFileClass>.SerializeToFile(MapFile, this.MapFileName);
        }
       
    }
}